 /**
 * Description: 内部单位
 * @author:Hunter
 * @since:1.0.0
 *
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-04-08   Hunter       1.0.0     Initial
 */
package com.metro.paperless.biz.service.impl;

import com.metro.core.dao.IMapper;
import com.metro.core.service.BaseServiceSupport;
import com.metro.core.service.ServiceException;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.metro.paperless.biz.mapper.IBizInsideOrgMapper;
import com.metro.paperless.biz.service.inf.IBizInsideOrgService;
import com.metro.paperless.sys.beans.vo.SysMaterialsType;
import com.metro.paperless.biz.beans.vo.BizInsideOrg;

 /**
 * 内部单位<br>
 *
 * @author:Hunter
 * @since:1.0.0
 * @version [1.0.0, 2018-04-08]
 */
@Service("bizinsideorgService")
public class BizInsideOrgService extends BaseServiceSupport<BizInsideOrg> implements IBizInsideOrgService{
    //内部单位Mapper
    @Autowired
    private IBizInsideOrgMapper bizinsideorgMapper;

    @Override
    public IMapper getMapper() {
        return bizinsideorgMapper;
    }

    @Override
    public IResult save(BizInsideOrg entity) {
        try {
            bizinsideorgMapper.save(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增内部单位失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult update(BizInsideOrg entity) {
        try {
            bizinsideorgMapper.updateNotEmpty(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改内部单位失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult delete(Long id) {
        try {
            bizinsideorgMapper.delete(id);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult deleteByIds(String ids) {
        try {
            bizinsideorgMapper.deleteByIds(ids);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
    /**
     * 判断内部单位是否已存在
     */
    public IResult checkInsideOrg(BizInsideOrg entity) {
    	//判断是否已存在
    	List<BizInsideOrg> list = bizinsideorgMapper.queryForList();
    	if(list != null && list.size()>0 ) {
    		for(int i=0;i<list.size();i++){
        		if(list.get(i).getOrgName().equals(entity.getOrgName()) && list.get(i).getDataStatus() == 1 ) {
        			return ResultCode.DUPLICATE_KEY;
        		}
        	}
    	}
    	return ResultCode.SUCCESS;
    }
    
    /**
     * 修改时判断内部单位是否已存在
     */
    public IResult checkUpdateInsideOrg(BizInsideOrg entity) {
    	//判断是否已存在
    	List<BizInsideOrg> list = bizinsideorgMapper.queryForList();
    	if(list != null && list.size()>0 ) {
    		for(int i=0;i<list.size();i++){
        		if(list.get(i).getOrgName().equals(entity.getOrgName()) && list.get(i).getPrincipal().equals(entity.getPrincipal()) && list.get(i).getLinkMobile().equals(entity.getLinkMobile()) && list.get(i).getDataStatus() == 1 ) {
        			return ResultCode.DUPLICATE_KEY;
        		}
        	}
    	}
    	return ResultCode.SUCCESS;
    }
    
    
    /**
     * 新增内部单位
     */
    @Override
    public IResult saveInsideOrg(BizInsideOrg entity) {
    	
    	Date date = new Date();
    	entity.setAddTime(date);
    	
        try {
            bizinsideorgMapper.save(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增内部单位失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
    
    /**
     * 修改内部单位
     */
    @Override
    public IResult updateInsideOrg(BizInsideOrg entity) {
        try {
            bizinsideorgMapper.updateNotEmpty(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改内部单位失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
    
}